Seaasdasd
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML"></script>
<!-- MathJax configuration -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true
},
// Center justify equations in code and markdown cells. Elsewhere
// we use CSS to left justify single line equations in code cells.
displayAlign: 'center',
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}},
linebreaks: { automatic: true }
}
});
</script>
<!-- End of mathjax configuration --></head>
柱形图¶
from pyecharts import options as opts
from pyecharts.charts import Bar
bar1 = (
# 图表类型
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [20, 30, 41, 15, 45, 100])
# 配置项内容
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)
bar1.render_notebook()
横向柱形图¶
from pyecharts import options as opts
from pyecharts.charts import Bar
def bar_reversal_axis(): # 返回的Bar的类实例
bar1 = (
# 图表类型
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [20, 30, 41, 15, 45, 100])
# 翻转
.reversal_axis()
# 系列配置项
.set_series_opts(label_opts=opts.LabelOpts(position='right'))
# 配置项内容
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)
return bar1
barh = bar_reversal_axis()
barh.render_notebook()
数据格式要求 不支持直接使用Series,必须 加上tolist()
折线图¶
from pyecharts import options as opts
from pyecharts.charts import Line
x = ['2018-{:0>2d}'.format(s) for s in range(1,13)]
y1 = [5,10,26,30,35,30,20,26,40,46,40,50]
y2 = [8,20,24,36,40,36,40,45,50,53,48,58]
bar1 = (
# 图表类型
Line()
.add_xaxis(x)
.add_yaxis("基金A", y1)
.add_yaxis("基金B", y2)
# 配置项内容
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)
bar1.render_notebook()
散点图¶
from pyecharts.charts import Scatter
import numpy as np
import pandas as pd
df = pd.DataFrame()
df['weight'] = np.array([56,67,65,70,57,60,80,85,76,64],dtype='int32')
df['height'] = np.array([162,170,168,172,168,172,180,176,178,170],dtype='int32')
df['height_m'] = [150,160,164,170,160,158,169,173,171,179]
scatter = Scatter()
scatter.add_xaxis(df['weight'])
scatter.add_yaxis('男生',df['height'])
scatter.add_yaxis('女生',df['height_m'])
scatter.set_global_opts(title_opts=opts.TitleOpts(title="Scatter-基本示例"))
scatter.render_notebook()
散点图数据支撑series类型
箱线图¶
from pyecharts import options as opts
from pyecharts.charts import Boxplot
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880]
+ [1000, 980, 930, 650, 760, 810, 1000, 1000, 960, 960],
[960, 940, 960, 940, 880, 800, 850, 880, 900]
+ [840, 830, 790, 810, 880, 880, 830, 800, 790, 760, 800],
]
v2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760]
+ [910, 920, 890, 860, 880, 720, 840, 850, 850, 780],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870]
+ [870, 810, 740, 810, 940, 950, 800, 810, 870],
]
c = Boxplot()
c.add_xaxis(["expr1", "expr2"])
c.add_yaxis("A", c.prepare_data(v1))
c.add_yaxis(
"B", c.prepare_data(v2) # 计算 中位数 最小值 最大值…
)
c.set_global_opts(title_opts=opts.TitleOpts(title="BoxPlot-基本示例"))
c.render_notebook()
饼图¶
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
Pie()
.add("", [list(z) for z in zip(Faker.choose(), Faker.values())],radius=["30%", "70%"],)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
)
c.render_notebook()
词云图¶
- jieba中文分词
- WordCloud词云库
from pyecharts.charts import WordCloud
words = ['python','jupyter','numpy','pandas','matplotlib','sklearn',
'xgboost','lightGBM','simpy','keras','tensorflow',
'hive','hadoop','spark']
counts = [100,90,65,95,50,60,70,70,20,70,80,80,60,60]
cloud = WordCloud()
cloud.add("文章",zip(words,counts))
cloud.render_notebook()
jieba库¶
import jieba
text = '据公告,该公司拟公开发行A股数量不超过12,093,342,392股(即不超过本次A股发行后公司已发行总股本的13%,超额配售选择权行使前)。公司可授权主承销商在符合法律法规及监管要求的前提下行使超额配售选择权,超额发售不超过本次发行A股股数(超额配售选择权行使前)15%的A股股份。所募资金将用于5G产业互联网建设项目、云网融合新型信息基础设施项目及科技创新研发项目。中国电信同日还发布了2020年中国电信发布2020年全年财报,公司2020年营运收入3935.6亿元人民币,净利润208.5亿元人民币,市场预估213.2亿元人民币;2020年每股盈利0.26元,市场预期0.27元,去年同期0.25元;5G套餐用户达到8650万户,渗透率24.6%。移动用户达到3.51亿户,净增1545万户,用户市场份额提升至22.0%。'
text_list = jieba.lcut(text)
arr = np.array(text_list)
arr
np.unique(arr,return_counts=True)
地图¶
Map¶
[list(z) for z in zip(Faker.provinces, Faker.values())]
s =[['广东', 28],
['北京', 64],
['上海', 85],
['江西', 65],
['湖南省', 139],
['湖南', 139],
['浙江', 121],
['江苏', 115]]
s
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
c = (
Map()
.add("商家A", s, "china") # [['北京',29],['上海',42]]
.set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"))
)
c.render_notebook()
Geo¶
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.faker import Faker
c = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Geo-基本示例")
)
)
c.render_notebook()
import math
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Line3D
data = []
for t in range(0, 25000):
_t = t / 1000
x = (1 + 0.25 math.cos(75 _t)) math.cos(_t)
y = (1 + 0.25 math.cos(75 _t)) math.sin(t)
z = t + 2.0 math.sin(75 t)
data.append([x, y, z])
c = (
Line3D()
.add(
"",
data,
xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type="value"),
yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type="value"),
grid3d_opts=opts.Grid3DOpts(width=100, height=100, depth=100),
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
max=30, min_=0, range_color=Faker.visual_color
),
title_opts=opts.TitleOpts(title="Line3D-基本示例"),
)
)
c.render_notebook()
import pyecharts.options as opts
from pyecharts.charts import Bar3D
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.baidu.com/examples/editor.html?c=bar3d-punch-card&gl=1
目前无法实现的功能:
1、光照和阴影暂时无法设置
"""
hours = [
"12a",
"1a",
"2a",
"3a",
"4a",
"5a",
"6a",
"7a",
"8a",
"9a",
"10a",
"11a",
"12p",
"1p",
"2p",
"3p",
"4p",
"5p",
"6p",
"7p",
"8p",
"9p",
"10p",
"11p",
]
days = ["Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"]
data = [
[0, 0, 5],
[0, 1, 1],
[0, 2, 0],
[0, 3, 0],
[0, 4, 0],
[0, 5, 0],
[0, 6, 0],
[0, 7, 0],
[0, 8, 0],
[0, 9, 0],
[0, 10, 0],
[0, 11, 2],
[0, 12, 4],
[0, 13, 1],
[0, 14, 1],
[0, 15, 3],
[0, 16, 4],
[0, 17, 6],
[0, 18, 4],
[0, 19, 4],
[0, 20, 3],
[0, 21, 3],
[0, 22, 2],
[0, 23, 5],
[1, 0, 7],
[1, 1, 0],
[1, 2, 0],
[1, 3, 0],
[1, 4, 0],
[1, 5, 0],
[1, 6, 0],
[1, 7, 0],
[1, 8, 0],
[1, 9, 0],
[1, 10, 5],
[1, 11, 2],
[1, 12, 2],
[1, 13, 6],
[1, 14, 9],
[1, 15, 11],
[1, 16, 6],
[1, 17, 7],
[1, 18, 8],
[1, 19, 12],
[1, 20, 5],
[1, 21, 5],
[1, 22, 7],
[1, 23, 2],
[2, 0, 1],
[2, 1, 1],
[2, 2, 0],
[2, 3, 0],
[2, 4, 0],
[2, 5, 0],
[2, 6, 0],
[2, 7, 0],
[2, 8, 0],
[2, 9, 0],
[2, 10, 3],
[2, 11, 2],
[2, 12, 1],
[2, 13, 9],
[2, 14, 8],
[2, 15, 10],
[2, 16, 6],
[2, 17, 5],
[2, 18, 5],
[2, 19, 5],
[2, 20, 7],
[2, 21, 4],
[2, 22, 2],
[2, 23, 4],
[3, 0, 7],
[3, 1, 3],
[3, 2, 0],
[3, 3, 0],
[3, 4, 0],
[3, 5, 0],
[3, 6, 0],
[3, 7, 0],
[3, 8, 1],
[3, 9, 0],
[3, 10, 5],
[3, 11, 4],
[3, 12, 7],
[3, 13, 14],
[3, 14, 13],
[3, 15, 12],
[3, 16, 9],
[3, 17, 5],
[3, 18, 5],
[3, 19, 10],
[3, 20, 6],
[3, 21, 4],
[3, 22, 4],
[3, 23, 1],
[4, 0, 1],
[4, 1, 3],
[4, 2, 0],
[4, 3, 0],
[4, 4, 0],
[4, 5, 1],
[4, 6, 0],
[4, 7, 0],
[4, 8, 0],
[4, 9, 2],
[4, 10, 4],
[4, 11, 4],
[4, 12, 2],
[4, 13, 4],
[4, 14, 4],
[4, 15, 14],
[4, 16, 12],
[4, 17, 1],
[4, 18, 8],
[4, 19, 5],
[4, 20, 3],
[4, 21, 7],
[4, 22, 3],
[4, 23, 0],
[5, 0, 2],
[5, 1, 1],
[5, 2, 0],
[5, 3, 3],
[5, 4, 0],
[5, 5, 0],
[5, 6, 0],
[5, 7, 0],
[5, 8, 2],
[5, 9, 0],
[5, 10, 4],
[5, 11, 1],
[5, 12, 5],
[5, 13, 10],
[5, 14, 5],
[5, 15, 7],
[5, 16, 11],
[5, 17, 6],
[5, 18, 0],
[5, 19, 5],
[5, 20, 3],
[5, 21, 4],
[5, 22, 2],
[5, 23, 0],
[6, 0, 1],
[6, 1, 0],
[6, 2, 0],
[6, 3, 0],
[6, 4, 0],
[6, 5, 0],
[6, 6, 0],
[6, 7, 0],
[6, 8, 0],
[6, 9, 0],
[6, 10, 1],
[6, 11, 0],
[6, 12, 2],
[6, 13, 1],
[6, 14, 3],
[6, 15, 4],
[6, 16, 0],
[6, 17, 0],
[6, 18, 0],
[6, 19, 0],
[6, 20, 1],
[6, 21, 2],
[6, 22, 2],
[6, 23, 6],
]
data = [[d[1], d[0], d[2]] for d in data]
(
Bar3D(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add(
series_name="",
data=data,
xaxis3d_opts=opts.Axis3DOpts(type_="category", data=hours),
yaxis3d_opts=opts.Axis3DOpts(type_="category", data=days),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
max_=20,
range_color=[
"#313695",
"#4575b4",
"#74add1",
"#abd9e9",
"#e0f3f8",
"#ffffbf",
"#fee090",
"#fdae61",
"#f46d43",
"#d73027",
"#a50026",
],
)
)
.render_notebook()
)
布局¶
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.faker import Faker
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
bar = (
Bar()
.add_xaxis(Faker.months)
.add_yaxis("蒸发量", v1)
.add_yaxis("降水量", v2)
.extend_axis(
yaxis=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} °C"), interval=5
)
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Overlap-bar+line"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} ml")),
)
)
line = Line().add_xaxis(Faker.months).add_yaxis("平均温度", v3, yaxis_index=1)
bar.overlap(line)
bar.render_notebook()
并行多图Grid¶
from pyecharts import options as opts
from pyecharts.charts import Bar, Geo, Grid
from pyecharts.faker import Faker
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(legend_opts=opts.LegendOpts(pos_left="20%"))
)
geo = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Grid-Geo-Bar"),
)
)
grid = (
Grid(init_opts=opts.InitOpts(width="900px",height="900px"))
.add(bar,grid_opts=opts.GridOpts(pos_left="10%",pos_top="80%",is_show=True))
.add(geo,grid_opts=opts.GridOpts(pos_left="90%",pos_top="5%"))
# .add(bar,grid_opts=opts.GridOpts(pos_left="10%",pos_top="80%"))
)
grid.render_notebook()
顺序多图¶
from pyecharts import options as opts
from pyecharts.charts import Bar, Geo, Page
from pyecharts.faker import Faker
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(legend_opts=opts.LegendOpts(pos_left="20%"))
)
geo = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Grid-Geo-Bar"),
)
)
page = (
Page()
.add(bar)
.add(geo)
)
page.render_notebook()
选项卡¶
from pyecharts import options as opts
from pyecharts.charts import Bar, Geo, Tab
from pyecharts.faker import Faker
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(legend_opts=opts.LegendOpts(pos_left="20%"))
)
geo = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Grid-Geo-Bar"),
)
)
tab = (
Tab()
.add(geo,"地图")
.add(bar,"柱形图")
)
tab.render_notebook()
时间线轮播¶
from pyecharts import options as opts
from pyecharts.charts import Pie, Timeline
from pyecharts.faker import Faker
attr = Faker.choose()
t1 = Timeline()
for i in range(2015,2021):
pie = (
Pie()
.add(
"商家A",
[list(z) for z in zip(attr,Faker.values())],
# rosetype="area",
# radius=["30%","55%"],
)
.set_global_opts(title_opts=opts.TitleOpts('某商店{}年营业额'.format(i)))
)
t1.add(pie,"{}年".format(i))
t1.render_notebook()




